//
//  DYMusicViewController.m
//  colorTest
//
//  Created by 丁益 on 2017/4/2.
//  Copyright © 2017年 celi. All rights reserved.
//  音乐模式

#import "DYMusicViewController.h"
#import "DYSendTestTool.h"
#import "SendWifiDataTool.h"
//#import "AsyncSocket.h"
#import "AsyncUdpSocket.h"
#import<AVFoundation/AVFoundation.h>
#import "SendWifiDataTool.h"


@interface DYMusicViewController ()
{
    SendWifiDataTool *_sendSingleColor;
    NSTimer *refreshColorTimer;
}

@property (nonatomic,strong) AsyncUdpSocket *socket;
@property (nonatomic,weak) UIButton *playMusic;
@property (nonatomic,strong)  AVAudioPlayer *play;  //播放器
@end

@implementation DYMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"七彩色";
    //self.view.backgroundColor = [UIColor blueColor];
    // Do any additional setup after loading the view.
    
    //创建播放试图
    CGFloat imageViewY =  80;
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, imageViewY, sWidth*0.68, sWidth*0.68)];
    imageView.centerX = sWidth/2;
    [imageView setImage:[UIImage imageNamed:@"img_cd"]];
    [self.view addSubview:imageView];
    
    UIButton *playMusic = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 120, 60)];
    playMusic.center = imageView.center;
    [playMusic setImage:[UIImage imageNamed:@"btn_play"] forState:UIControlStateNormal];
    [playMusic setImage:[UIImage imageNamed:@"btn_play01"] forState:UIControlStateHighlighted];
    [playMusic setImage:[UIImage imageNamed:@"btn_pause01"] forState:UIControlStateSelected];
    
    [playMusic addTarget:self action:@selector(playMusicOnclick:) forControlEvents:UIControlEventTouchUpInside];
    self.playMusic = playMusic;
    [self.view addSubview:playMusic];
    
    
    CGFloat musicLabelY = CGRectGetMaxY(imageView.frame)+2;
    UILabel *musicLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, musicLabelY, sWidth, 80)];
    musicLabel.centerX = sWidth/2;
    musicLabel.numberOfLines = 0;
    musicLabel.text = @"歌名:《怒放的生命》 歌手:汪峰";
    musicLabel.font = [UIFont systemFontOfSize:14];
    musicLabel.textColor = [UIColor blackColor];
    musicLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview: musicLabel];
    
    
    CGFloat selectBtnY = CGRectGetMaxY(musicLabel.frame);
    UIButton *selectBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,selectBtnY,100,30)];
    selectBtn.centerX = sWidth/2;
    [selectBtn setTitle:@"选择音乐" forState:UIControlStateNormal];
    [selectBtn addTarget:self action:@selector(selectBtnOnclick:)
        forControlEvents:UIControlEventTouchUpInside];
    selectBtn.contentMode = UIViewContentModeCenter;
    [selectBtn setTitleColor:[UIColor  blackColor] forState:UIControlStateNormal];
//    selectBtn.backgroundColor = [UIColor redColor];
    [self.view addSubview:selectBtn];
    
    
    CGFloat leftImageWidth = 36;
    CGFloat leftImageArrowX = CGRectGetMinX(selectBtn.frame)-leftImageWidth;
    UIButton *leftImageArrow = [[UIButton alloc]initWithFrame:CGRectMake(leftImageArrowX, selectBtnY, leftImageWidth, 28)];
    [leftImageArrow setImage:[UIImage imageNamed:@"btn_prev1"] forState: UIControlStateNormal];
    [leftImageArrow setImage:[UIImage imageNamed:@"btn_prev2"] forState:UIControlStateHighlighted];
    [leftImageArrow addTarget:self action:@selector(leftImageArrowOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftImageArrow];
    
    
    CGFloat rightImageArrowX = CGRectGetMaxX(selectBtn.frame);
    UIButton *rightImageArrow = [[UIButton alloc]initWithFrame:CGRectMake(rightImageArrowX, selectBtnY, leftImageWidth, 28)];
    [rightImageArrow setImage:[UIImage imageNamed:@"btn_next1"] forState: UIControlStateNormal];
    [rightImageArrow setImage:[UIImage imageNamed:@"btn_next2"] forState:UIControlStateHighlighted];
    [rightImageArrow addTarget:self action:@selector(rightImageArrowOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightImageArrow];
    
    
    NSString *path1 = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"mp3"];
    NSURL *url1 = [NSURL fileURLWithPath:path1];
    
    //默认歌曲
    self.play= [[AVAudioPlayer alloc]initWithContentsOfURL:url1 error:nil];
   [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    //默认音量
    self.play.volume = 1.0;
    
    
    SendWifiDataTool *sendSingleColor = [SendWifiDataTool sharedSendWifiDataTool];
    _sendSingleColor = sendSingleColor;
    
    
    [self creatTimer];
}


#pragma 按键处理事件
- (void)playMusicOnclick:(UIButton*)btn
{
    
    
    
    DLog(@"playMusicOnclick--");
    self.playMusic.selected = !btn.selected;
    if (btn.selected) {
        [self.play play];
        [self startTimer];
        
    } else {
        [self.play stop];
        [self stopTimer];
    }
}

- (void)selectBtnOnclick:(UIButton*)btn
{
    DLog(@"selectBtnOnclick----");
    
}

- (void)leftImageArrowOnclick:(UIButton*)btn
{
    DLog(@"leftImageArrowOnclick----");
}

- (void)rightImageArrowOnclick:(UIButton*)btn
{
    DLog(@"rightImageArrowOnclick----");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark -定时器
- (void)stopTimer
{
    //关闭定时器
    [refreshColorTimer setFireDate:[NSDate distantFuture]];
}


- (void)startTimer
{
    //开启定时器
    [refreshColorTimer setFireDate:[NSDate distantPast]];
}

- (void)creatTimer
{
    if ([refreshColorTimer isValid]) {
        [refreshColorTimer invalidate];
    }
    refreshColorTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(willRefreshScreen) userInfo:nil repeats:YES];
    [self stopTimer];
}


- (void)willRefreshScreen
{
    
    DLog(@"%i",arc4random_uniform(255));
    
    NSString *redValue = [NSString stringWithFormat:@"%i",ARC4RANDOM_COLOR];
    NSString *greenValue = [NSString stringWithFormat:@"%i",ARC4RANDOM_COLOR];
    NSString *blueValue = [NSString stringWithFormat:@"%i",ARC4RANDOM_COLOR];
    
    [_sendSingleColor  sendSingleRGBColorCmdRedValue: redValue blueValue:blueValue greenValue:greenValue];
}


@end
